package view.principal;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JSeparator;
import javax.swing.LayoutStyle.ComponentPlacement;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import utils.Criptografia;
import model.entidades.Usuario;
import controller.ControladorExcecao;
import controller.ControladorUsuario;


public class TelaAlterarSenha extends JDialog implements ActionListener {

	private static final long serialVersionUID = 1L;

	private JPanel panelNorte;
	private final JPanel panelCentro = new JPanel();
	private JPanel panelSul;

	private JLabel lblAlterarSenhaUsuario;
	private JLabel lblConfirmacaoNovaSenha;
	private JLabel label;
	private JLabel lblSenhaAtual;
	private JLabel lblChave;
	private JLabel lblNovaSenha;

	private JPasswordField txtConfirmaNovaSenha;
	private JPasswordField txtSenhaAtual;
	private JPasswordField txtNovaSenha;

	private JSeparator separator;

	private JButton btnSalvar;
	private JButton btnCancelar;

	private Usuario usuarioLogado;

	public TelaAlterarSenha(Usuario usuario) {
		this.usuarioLogado = usuario;
		inicializaComponentes();
	}

	private void inicializaComponentes() {
		setBounds(100, 100, 350, 310);
		setResizable(false);
		setModal(true);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		panelCentro.setBackground(Color.WHITE);
		panelCentro.setBorder(new EmptyBorder(5, 0, 5, 5));
		getContentPane().add(panelCentro, BorderLayout.CENTER);

		lblNovaSenha = new JLabel("Nova Senha:");

		txtConfirmaNovaSenha = new JPasswordField();
		txtConfirmaNovaSenha.setEchoChar('●');
		txtConfirmaNovaSenha.setColumns(10);

		lblConfirmacaoNovaSenha = new JLabel("Confirma\u00E7\u00E3o de Nova Senha:");

		txtNovaSenha = new JPasswordField();
		txtNovaSenha.setEchoChar('●');
		txtNovaSenha.setColumns(10);

		lblSenhaAtual = new JLabel("Senha Atual:");

		txtSenhaAtual = new JPasswordField();
		txtSenhaAtual.setEchoChar('●');
		txtSenhaAtual.setColumns(10);

		separator = new JSeparator();

		lblChave = new JLabel("");
		lblChave.setIcon(new ImageIcon(TelaAlterarSenha.class.getResource("/resources/images/ic_vpn_key_black_24dp.png")));

		label = new JLabel("");
		label.setVerticalAlignment(SwingConstants.BOTTOM);
		label.setIcon(new ImageIcon(TelaAlterarSenha.class.getResource("/resources/images/ic_lock_black_24dp.png")));
		label.setIconTextGap(0);
		label.setHorizontalTextPosition(SwingConstants.LEADING);
		label.setBorder(null);
		label.setHorizontalAlignment(SwingConstants.CENTER);
		GroupLayout gl_panelCentro = new GroupLayout(panelCentro);
		gl_panelCentro.setHorizontalGroup(
			gl_panelCentro.createParallelGroup(Alignment.TRAILING)
				.addGroup(gl_panelCentro.createSequentialGroup()
					.addGroup(gl_panelCentro.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelCentro.createSequentialGroup()
							.addGap(9)
							.addGroup(gl_panelCentro.createParallelGroup(Alignment.LEADING, false)
								.addComponent(lblSenhaAtual)
								.addComponent(txtSenhaAtual, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE))
							.addPreferredGap(ComponentPlacement.UNRELATED)
							.addComponent(lblChave))
						.addGroup(gl_panelCentro.createSequentialGroup()
							.addContainerGap()
							.addGroup(gl_panelCentro.createParallelGroup(Alignment.LEADING)
								.addComponent(txtConfirmaNovaSenha, GroupLayout.PREFERRED_SIZE, 182, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblNovaSenha)
								.addComponent(txtNovaSenha, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE)
								.addComponent(lblConfirmacaoNovaSenha))
							.addPreferredGap(ComponentPlacement.RELATED, 9, Short.MAX_VALUE)
							.addComponent(label, GroupLayout.PREFERRED_SIZE, 78, GroupLayout.PREFERRED_SIZE)))
					.addContainerGap())
				.addGroup(gl_panelCentro.createSequentialGroup()
					.addGap(9)
					.addComponent(separator, GroupLayout.DEFAULT_SIZE, 322, Short.MAX_VALUE)
					.addGap(3))
		);
		gl_panelCentro.setVerticalGroup(
			gl_panelCentro.createParallelGroup(Alignment.LEADING)
				.addGroup(gl_panelCentro.createSequentialGroup()
					.addContainerGap()
					.addGroup(gl_panelCentro.createParallelGroup(Alignment.LEADING, false)
						.addGroup(gl_panelCentro.createSequentialGroup()
							.addComponent(lblSenhaAtual)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtSenhaAtual, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(lblChave, 0, 0, Short.MAX_VALUE))
					.addGap(12)
					.addComponent(separator, GroupLayout.PREFERRED_SIZE, 2, GroupLayout.PREFERRED_SIZE)
					.addGap(12)
					.addGroup(gl_panelCentro.createParallelGroup(Alignment.LEADING)
						.addGroup(gl_panelCentro.createSequentialGroup()
							.addComponent(lblNovaSenha)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtNovaSenha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(lblConfirmacaoNovaSenha)
							.addPreferredGap(ComponentPlacement.RELATED)
							.addComponent(txtConfirmaNovaSenha, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
						.addComponent(label, GroupLayout.DEFAULT_SIZE, 106, Short.MAX_VALUE))
					.addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
		);
		gl_panelCentro.linkSize(SwingConstants.HORIZONTAL, new Component[] {txtConfirmaNovaSenha, txtNovaSenha, txtSenhaAtual});
		panelCentro.setLayout(gl_panelCentro);
		{
			panelSul = new JPanel();
			panelSul.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(panelSul, BorderLayout.SOUTH);
			{
				btnSalvar = new JButton("Salvar");
				btnSalvar.setActionCommand("OK");
				btnSalvar.addActionListener(this);
				panelSul.add(btnSalvar);
				getRootPane().setDefaultButton(btnSalvar);
			}
			{
				btnCancelar = new JButton("Cancelar");
				btnCancelar.setActionCommand("Cancel");
				btnCancelar.addActionListener(this);
				panelSul.add(btnCancelar);
			}
		}
		{
			panelNorte = new JPanel();
			panelNorte.setForeground(Color.WHITE);
			panelNorte.setBackground(Color.BLUE);
			getContentPane().add(panelNorte, BorderLayout.NORTH);

			lblAlterarSenhaUsuario = new JLabel("Alterar Senha de Usu\u00E1rio");
			lblAlterarSenhaUsuario.setForeground(Color.WHITE);
			lblAlterarSenhaUsuario.setFont(new Font("Tahoma", Font.BOLD, 18));
			panelNorte.add(lblAlterarSenhaUsuario);
		}
	}//inicializaComponentes()

	private void alterarSenha() {
		ControladorUsuario controladorUsuario = new ControladorUsuario(null);
		String senhaAtual = Criptografia.criptografaSenha(new String(txtSenhaAtual.getPassword()));
		if (senhaAtual.equals(usuarioLogado.getSenha())) {
			String novaSenha = Criptografia.criptografaSenha(new String(txtNovaSenha.getPassword()));
			String confirmaSenha = Criptografia.criptografaSenha(new String(txtConfirmaNovaSenha.getPassword()));
			if(novaSenha.equals(confirmaSenha)){
				usuarioLogado.setSenha(novaSenha);
				controladorUsuario.alterarSenha(usuarioLogado);
				JOptionPane.showMessageDialog(this, "A senha foi alterada com sucesso!", "Alteração de Senha", JOptionPane.PLAIN_MESSAGE );
				this.dispose();
			} else {
				JOptionPane.showMessageDialog(this, ControladorExcecao.SENHAS_DIVERGENTE, "Erro de Validação", JOptionPane.OK_OPTION);
			}
		} else {
			JOptionPane.showMessageDialog(this, "A senha atual não é válida!", "Erro de Validação", JOptionPane.OK_OPTION);
		}
	}//alterarSenha()
	
	@Override
	public void actionPerformed(ActionEvent e) {

		String comando = e.getActionCommand();

		if (comando.equals(btnSalvar.getActionCommand())) {
			alterarSenha();
		} else if (comando.equals(btnCancelar.getActionCommand())) {
			this.dispose();
		}

	}//actionPerformed()
	
}//TelaAlterarSenha.class
